package com.icez.base_util_library

import android.app.Activity
import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.view.*
import java.lang.RuntimeException


class WindowManagerUtil {
    companion object{
        val instance:WindowManagerUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){
            WindowManagerUtil()
        }

        //针对的view
        var TARGETVIEW: View?= null
        //窗口管理
        var WINDOWMANAGER:WindowManager ?= null
        //上下文
        var CONTEXT:Context ?= null
    }

    /**
     *  初始化悬浮窗口
     * @param 上下文
     */
    public fun initFloatConfig(context: Context?){
        CONTEXT = context
        WINDOWMANAGER = context?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }

    /**
     *  添加悬浮窗口
     * @param layoutId 布局
     * @param setBusiness 业务处理
     * @param setParams 设置参数
     */
    public fun addFloatWindow(layoutId:Int?,setParams:(()->Unit)?=null,setBusiness:((layoutView:View?)->Unit)?=null){
        if(CONTEXT==null){
            throw RuntimeException("请先在Application中初始化，initFloatConfig")
        }
        removeFloatWindow()
        TARGETVIEW = layoutId?.let { LayoutInflater.from(CONTEXT).inflate(it,null,false) }
        var layoutParam = WindowManager.LayoutParams().apply {
            //宽度自适应
            width = ViewGroup.LayoutParams.WRAP_CONTENT
            //高度自适应
            height = ViewGroup.LayoutParams.WRAP_CONTENT
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else {
                type = WindowManager.LayoutParams.TYPE_PHONE
            }
            // 背景透明
            format = PixelFormat.TRANSLUCENT
            // 设置外部可以点击
            flags =
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            // 设置布局位置
            gravity = Gravity.TOP or Gravity.LEFT
            x = x + 10

            var startRawX:Float = 0f
            var startRawY:Float = 0f
            var lp = this
            TARGETVIEW?.setOnTouchListener(object:View.OnTouchListener{
                override fun onTouch(p0: View?, motionEvent: MotionEvent?): Boolean {
                    when (motionEvent?.getAction()) {
                        MotionEvent.ACTION_DOWN -> {
                            startRawX = motionEvent.rawX
                            startRawY = motionEvent.rawY
                        }
                        MotionEvent.ACTION_MOVE -> {
                            val movedX: Float = motionEvent.rawX - startRawX
                            val movedY: Float = motionEvent.rawY - startRawY
                            startRawX = motionEvent.rawX
                            startRawY = motionEvent.rawY
                            lp.x = (lp.x + movedX).toInt()
                            lp.y = (lp.y + movedY).toInt()
                            WINDOWMANAGER?.updateViewLayout(TARGETVIEW, lp)
                        }
                        else -> {}
                    }
                    return false
                }

            })
            if (setParams != null) {
                setParams()
            }
        }
        if (setBusiness != null) {
            setBusiness(TARGETVIEW)
        }
        WINDOWMANAGER?.addView(TARGETVIEW,layoutParam)
    }

    /**
     *  移除activity的当前窗口
     * @param activity 上下文
     */
    public fun removeFloatWindow(){
        if(CONTEXT==null){
            throw RuntimeException("请先在Application中初始化，initFloatConfig")
        }
        if(SystemUtil.instance.isExitAppLayout(TARGETVIEW)){
            WINDOWMANAGER?.removeView(TARGETVIEW)
            TARGETVIEW = null
        }
    }


}